package com.andnnl.annotation;

import com.andnnl.annotation.pojo.IPeople;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;

/**
 * Created by chenss on 2017/9/30.
 */
public class AnnotationUtil {
    public static void main(String[] args) throws InterruptedException {
//        System.out.println(BlackPeople.class.getPackage());
        // 获取特定包下所有的类(包括接口和类)
//        List<Class<?>> clsList = ClassUtil.getAllClassByPackageName(BlackPeople.class.getPackage());
        List<Class<?>> clsList = ClassUtil.getClasses("com.andnnl");


        //输出所有使用了特定注解的类的注解值
        validMethodAnnotation(clsList);

        Thread.currentThread().sleep(10000);
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        clsList = ClassUtil.getClasses("com.andnnl");
        //输出所有使用了特定注解的类的注解值
        validMethodAnnotation(clsList);
    }




    public static void validMethodAnnotation(List<Class<?>> clsList){
        if (clsList != null && clsList.size() > 0) {
            for (Class<?> cls : clsList) {

//                cls.getDeclaredClasses();
                GatewayNumAnnotion gatewayNumAnnotion =cls.getAnnotation(GatewayNumAnnotion.class);
                if(gatewayNumAnnotion !=null){
                    System.out.println(gatewayNumAnnotion.value());
                }
                //获取类中的所有的方法
                Method[] methods = cls.getDeclaredMethods();
                if (methods != null && methods.length > 0) {
                    for (Method method : methods) {
                        PeopleAnnotion peopleAnnotion = (PeopleAnnotion) method.getAnnotation(PeopleAnnotion.class);
                        if (peopleAnnotion != null) {
                            //可以做权限验证
                            System.out.println(peopleAnnotion.say()+"\t"+method.getDeclaringClass());
                            boolean isAbstract = Modifier.isAbstract(cls.getModifiers());
                            boolean isInterface = Modifier.isInterface(cls.getModifiers());
//                            try {
//                                if(!isAbstract && !isInterface) {
//                                    IPeople instanceObj = (IPeople) cls.newInstance();
//                                    instanceObj.say();
//                                    instanceObj.walk();
//                                }
//                            } catch (InstantiationException e) {
//                                e.printStackTrace();
//                            } catch (IllegalAccessException e) {
//                                e.printStackTrace();
//                            }
                        }
                    }
                }
            }
        }
    }
}
